package com.ay.weixin.fdc.front.scratchcard.event;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;

import org.lizar.controller.Event;
import org.lizar.controller.loader.EventLoader;
import org.lizar.ioc.Container;

import com.ay.weixin.fdc.boss.user.db.WeiXinInfoDao;
import com.ay.weixin.fdc.boss.user.domain.UserWeiXinInfo;
import com.ay.weixin.fdc.front.scratchcard.db.ScratchCardDao;
import com.ay.weixin.fdc.front.scratchcard.db.ScratchRewardDao;
import com.ay.weixin.fdc.front.scratchcard.db.UserScratchInfoDao;
import com.ay.weixin.fdc.front.scratchcard.domain.ScratchCard;
import com.ay.weixin.fdc.front.scratchcard.domain.ScratchReward;
import com.ay.weixin.fdc.front.scratchcard.domain.UserScratchInfo;
import com.ay.weixin.fdc.front.usercenter.event.UserInfoAccess;

public class ScratchNow extends Event {

	private UserInfoAccess user_access;
	
	private WeiXinInfoDao weixin_info_dao;
	
	private ScratchCardDao scratch_card_dao;
	
	private ScratchRewardDao scratch_reward_dao;
	
	private UserScratchInfoDao user_info_dao;
	
	@Override
	public void init_property() throws Exception {
		user_access=Container.get(UserInfoAccess.class);
		weixin_info_dao=Container.get(WeiXinInfoDao.class);
		scratch_card_dao=Container.get(ScratchCardDao.class);
		scratch_reward_dao=Container.get(ScratchRewardDao.class);
		user_info_dao=Container.get(UserScratchInfoDao.class);
	}

	@Override
	public String default_handling_path() {
		return "/scratch_now/*";
	}

	@Override
	public void handle(EventLoader el) throws ServletException, IOException {
		long project_id=el.queryPathInt(1);
		System.out.println("project_id:"+project_id);
		
		if(el.queryPathCheck(2, "save_scratch")){
			try {
				save_scratch(el);
			} catch (SQLException e) {
				e.printStackTrace();
				el.json("{msg:'failed',status:2,already_times:0}");
			}
		}else if(el.queryPathCheck(2, "test")){
			try {
				test(el,project_id);
			} catch (SQLException e) {
				e.printStackTrace();
				el.template("/WEB-INF/template/app/"+project_id+"/activity/scratch/500.vm");
			}
		}else{
			try {
				scratch(el,project_id);
			} catch (SQLException e) {
				e.printStackTrace();
				el.template("/WEB-INF/template/app/"+project_id+"/activity/scratch/500.vm");
			}
		}
	}
	
	private void test(EventLoader el,long project_id) throws SQLException, IOException, ServletException{
		long id=el.getLong("scratcher");
		
		
		
		
		ScratchCard scratch_card=scratch_card_dao.get(id);
		
		if(scratch_card==null){
			el.template("/WEB-INF/template/app/"+project_id+"/activity/scratch/404.vm");
			return;
		}
		
		List<ScratchReward> reward_list=scratch_reward_dao.getRewards(id);
		
		String openid="o4cKjjsfUQxK4QjFTtG46X5E0ahg";
		
		UserScratchInfo user_info=user_info_dao.getByOpenId(openid,id);
		
		
		if(user_info==null){
			user_info=new UserScratchInfo();
			user_info.setOpenid(openid);
			user_info.setReward_id(0);
			user_info.setScratch_id(id);
			user_info.setScratch_times(0);
			user_info_dao.insert(user_info);
		}
		
		if(user_info.getReward_id()!=-1){
			for(ScratchReward reward:reward_list){
				if(reward.getId()==user_info.getReward_id()){
					el.setAttr("user_reward",reward);
					break;
				}
			}
		}
		
		
		
		el.setAttr("reward_list",reward_list);
			
		el.setAttr("user_info",user_info);
		
		el.setAttr("scratch_card",scratch_card);
		
		el.template("/WEB-INF/template/app/"+project_id+"/activity/scratch/index.vm");
	}
	
	private void save_scratch(EventLoader el) throws SQLException, IOException{
		long user_info_id=el.getLong("user_info_id");
		
		long reward_id=0;
		
		UserScratchInfo user_info=user_info_dao.get(user_info_id);
		
		if(user_info==null){
			el.json("{msg:'not exists',status:0,already_times:0}");
			return;
		}
		
		System.out.println(user_info_id);
		user_info_dao.imc_scratch(user_info_id,reward_id);
		
		
		
		el.json("{msg:'success',status:1,already_times:"+(user_info.getScratch_times()+1)+"}");
		
	}

	private void scratch(EventLoader el,long wxproject_id) throws IOException, ServletException, SQLException{
		long id=el.getLong("scratcher");
		
		UserWeiXinInfo weixin_info=el.getSessionAttr("weixin_info",UserWeiXinInfo.class);
		if(weixin_info==null||weixin_info.getSubscribe()!=1){
			user_access.open_id_check(el,"/scratch_now/",wxproject_id,"?scratcher="+id);
			return;
		}
		
		
		ScratchCard scratch_card=scratch_card_dao.get(id);
		
		if(scratch_card==null){
			el.template("/WEB-INF/template/app/"+wxproject_id+"/activity/scratch/404.vm");
			return;
		}
		
		List<ScratchReward> reward_list=scratch_reward_dao.getRewards(id);
		
		UserScratchInfo user_info=user_info_dao.getByOpenId(weixin_info.getId(),id);
		
		
		if(user_info==null){
			user_info=new UserScratchInfo();
			user_info.setOpenid(weixin_info.getId());
			user_info.setReward_id(0);
			user_info.setScratch_id(id);
			user_info.setScratch_times(0);
			user_info_dao.insert(user_info);
		}
		
		if(user_info.getReward_id()!=-1){
			for(ScratchReward reward:reward_list){
				if(reward.getId()==user_info.getReward_id()){
					el.setAttr("user_reward",reward);
					break;
				}
			}
		}
		
		
		
		
		el.setAttr("weixin_info",weixin_info);
		
		el.setAttr("reward_list",reward_list);
			
		el.setAttr("user_info",user_info);
		
		el.setAttr("scratch_card",scratch_card);
		
		el.template("/WEB-INF/template/app/"+wxproject_id+"/activity/scratch/index.vm");
	}
	
}
